import { Camera } from './Camera';

export class GNode {
  protected parent: GNode | null = null;
  private children: GNode[] = [];

  public position: { x: number; y: number } = { x: 0, y: 0 };

  constructor() {}

  setParent(parent: GNode | null): void {
    this.parent = parent;
  }

  addChild(child: GNode): void {
    this.children.push(child);
    child.setParent(this);
  }

  removeChild(child: GNode): void {
    const index = this.children.indexOf(child);
    if (index !== -1) {
      this.children.splice(index, 1);
      child.setParent(null);
    }
  }

  update(deltaTime: number): void {
    this.children.forEach(child => child.update(deltaTime));
  }

  render(context: CanvasRenderingContext2D, camera: Camera): void {
    this.children.forEach(child => child.render(context, camera));
  }
}
